<script>
import { updateClass } from '@/api/admin/class.js'
import classRules from '@/rules/classRules.js'
import { selectMajorOptions } from '@/api/authApi.js'
export default {
  props: ['modelValue'],
  emits: ['update:modelValue', 'resetData'],
  computed: {
    flag: {
      get() {
        return this.modelValue
      },
      set(nv) {
        this.$emit('update:modelValue', nv)
      },
    },
  },
  data() {
    return {
      classRules,
      majors: [],
      params: {
        major_id: '',
      },
      classData: {
        class_id: '',
        class_grade: '',
        class_name: '',
        class_major_id: '',
      },
    }
  },

  methods: {
    async initMajorOptions() {
      this.majors = await selectMajorOptions(this.params)
    },
    remoteMethod(key) {
      this.params.major_id = key
      this.initMajorOptions()
    },
    async EditNewClass() {
      await this.$refs.form.validate()
      await updateClass(this.classData)
      this.flag = false
      this.$emit('resetData')
    },
    resetFormData() {
      this.classData = {}
      this.$refs.form.resetFields()
    },
    setDefaultForm(obj) {
      obj.class_id = obj.class_id
      obj.class_major_id = obj.major_id
      obj.class_grade = obj.class_grade
      obj.class_name = obj.class_name
      this.classData = obj
    },
  },
  created() {
    this.initMajorOptions()
  },
}
</script>

<template>
  <el-dialog v-model="flag" title="老师信息更新" width="600px" @closed="resetFormData">
    <el-form label-width="80px" :inline="false" ref="form" :model="classData" :rules="classRules">
      <el-form-item label="班级编号" prop="class_id">
        <el-input type="text" v-model="classData.class_id"></el-input>
      </el-form-item>
      <el-form-item label="年级" prop="class_grade">
        <el-input type="text" v-model="classData.class_grade"></el-input>
      </el-form-item>
      <el-form-item label="班级名称" prop="class_name">
        <el-input type="text" v-model="classData.class_name"></el-input>
      </el-form-item>
      <el-form-item label="专业" prop="class_major_id">
        <el-select
          clearable
          v-model="classData.class_major_id"
          filterable
          remote
          :remote-method="remoteMethod"
        >
          <el-option
            v-for="m in majors"
            :key="m.major_id"
            :value="m.major_id"
            :label="m.major_name"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="$emit('update:modelValue', nv)">取消</el-button>
      <el-button type="primary" @click="EditNewClass()">更新</el-button>
    </template>
  </el-dialog>
</template>
